package com.spongebob.controller.ly;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.spongebob.pojo.*;
import com.spongebob.service.VisitorInfoService;
import com.spongebob.service.VstmessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import javax.xml.crypto.Data;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tutu
 * @since 2021-03-24
 */
@Controller

public class VstmessageController {
    @Autowired
    private VisitorInfoService visitorInfoService;
    @Autowired
    private VstmessageService vstmessageService;
    @RequestMapping("/vschats")
    public String vschats(Model model, HttpSession session,@RequestParam("ip") String ip){
        model.addAttribute("status",577);
        System.out.println("将要对话的访客id为"+ip);
        QueryWrapper qw =new QueryWrapper();
        qw.eq("ip",ip);
        System.out.println("```````````````````````````");
        VisitorInfo visitorInfoc= visitorInfoService.getOne(qw);
        System.out.println("根据ip查询到的访客为"+visitorInfoc);


        model.addAttribute("vschatvs",visitorInfoc);

        QueryWrapper qw2 =new QueryWrapper();
        System.out.println("对ip进行message查询"+ip);
        qw2.eq("ip",ip);


        List<Vstmessage>vsm=vstmessageService.list(qw2);
        for (Vstmessage s : vsm) {
            System.out.println("根据ip获得的信息为"+s.getContent());
        }


        session.setAttribute("vsip",ip);
        session.setAttribute("vscon",vsm);


        return "index";
    }
    @RequestMapping("/vschats2")
    public String vschats2(Model model, HttpSession session){
        String ip=session.getAttribute("vsip").toString();
        model.addAttribute("status",577);
        System.out.println("将要对话的访客id为"+ip);
        QueryWrapper qw =new QueryWrapper();
        qw.eq("ip",ip);

        VisitorInfo visitorInfoc= visitorInfoService.getOne(qw);



        model.addAttribute("vschatvs",visitorInfoc);

        QueryWrapper qw2 =new QueryWrapper();

        qw2.eq("ip",ip);


        List<Vstmessage>vsm=vstmessageService.list(qw2);
        for (Vstmessage s : vsm) {
            System.out.println("根据ip获得的信息为"+s.getContent());
        }


        session.setAttribute("vsip",ip);
        session.setAttribute("vscon",vsm);


        return "index";
    }
    @RequestMapping("/vschats/submit")
    public String vssub(Model model,String content,HttpSession session){

        String ip=session.getAttribute("vsip").toString();
        if (content.isEmpty()) {
            return "redirect:/vschats2";
        }

        Vstmessage vstmessage= new Vstmessage();
        vstmessage.setContent(content);

        vstmessage.setCcsendtime(new Date());

        vstmessage.setFromvisitor(0);
        String vsip1=session.getAttribute("vsip").toString();
        vstmessage.setIp(vsip1);

        vstmessageService.save(vstmessage);
        return "redirect:/vschats2";

    }

}

